package com.snack.common.result;

/**
 * @author huangfuhao
 * @Date 2024/10/28
 */

/**
 * Result，返回结果封装
 * 统一返回code、msg、data三种信息
 * code只提供3种类型，分别为成功200，失败-1，未登录-2
 */
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {
    public static int CODE_OK = 200; //成功
    public static int CODE_ERROR = -1;  //失败
    private Integer code; //编码：200成功，0和其它数字为失败
    private String msg; //错误信息
    private T data; //数据


    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.code = CODE_OK;
        return result;
    }

    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.data = object;
        result.code = CODE_OK;
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = CODE_ERROR;
        return result;
    }
    public static <T> Result<T> unauthorizel(String msg)
    {
        Result result = new Result();
        result.msg = msg;
        result.code = HttpStatus.UNAUTHORIZED.value();
        return  result;
    }
    public static <T> Result<T> forbidden(String msg)
    {
        Result result = new Result();
        result.msg = msg;
        result.code = HttpStatus.FORBIDDEN.value();
        return  result;
    }
}
